﻿var currentAttachment = null;

if ($.getQueryString("id") == null)
{
    $("[name=attachment]").hide();
}


Attachment = new Object();
Attachment.AddButton = $("<div class='col-md-3 form-group addAttachmentButton' attachmentToolType=\'AddButton\' style='padding-left:25px;padding-right:25px;'>\
                                <div style='width:100%;' >\
                                    <label for='' onclick='Attachment.StyleJS.creatediv(this);' class='' title='' style='cursor:pointer;border:1px solid #CCC;margin-right:10px;height:100px;width:100%;font-size:30px;text-align:center;line-height:100px;'>+</label>\
                                </div>\
                            </div>");


////////////////////////////////////////样式相关JS////////////////////////////////////////////////////////

Attachment.StyleJS = new Object();


Attachment.StyleJS.showbtn = function (d) {
    d.getElementsByTagName('div')[1].style.display = 'block';
}
Attachment.StyleJS.hidebtn = function (d) {
    d.getElementsByTagName('div')[1].style.display = 'none';
}
Attachment.StyleJS.changediv = function (data) {
    var v = document.getElementById('AddAttachment');
    var src = data.parentNode.parentNode.getElementsByTagName('img')[0].src;
    document.getElementById("att_image_add").src = src;
    document.getElementById('shade').style.display = "block";
    v.style.display = "block";
}
Attachment.StyleJS.creatediv = function (data) {
    var v = document.getElementById('AddAttachment');
    document.getElementById('shade').style.display = "block";
    $("#att_attflag_add").val($(data).parent().parent().parent().attr("attflag"));
    $("#att_image_add").attr("src", "");
    $("#att_name_add").val("");
    $("#att_desc_add").val("");
    v.style.display = "block";
}
Attachment.StyleJS.hidediv = function (data) {
    var v = document.getElementById('AddAttachment');
    document.getElementById('shade').style.display = "none";
    v.style.display = "none";
}


////////////////////////////////////////////////////////////


var viewers = new Array();
function initViewer() {
    for (var i = 0; i < viewers.length; i++) {
        viewers[i].destroy();
    }
    viewers.length = 0;
    $('[name=attachment]').each(function () {
        var flag = $(this).attr("attflag")
        var viewer = new Viewer($(this)[0], {
            url: flag
        });
        viewers.push(viewer);
    })
}




AttachmentManagement();
function imgchange() {
    $.ajaxFileUpload({
        url: "/Service/Attachment/AddAttachment.ashx",
        postData: { "json": "" },
        type: 'post',
        cache: false,
        dataType: "text",
        fileElementId: 'ImportFile',
        success: function (data) {
            var reg = /{\S*\s*?}/g;
            var result = data.match(reg);
            var url = JSON.parse(result).Result;
            $("#att_image_add").attr("src", url);
            $("#att_image_add").attr("realsrc", url);
        },
        error: function (e) {
            alert("上传文件出错！");
        }
    });
}


//初始化Attachment
function AttachmentManagement() {

    Attachments = !!Attachments ? Attachments : [];
    for (var i = 0; i < Attachments.length; i++) {
        var templeteContent = $("[name=attachment][attflag=" + Attachments[i].AttFlag + "]");
        if (templeteContent.children().length > 0) {
            var templete = $(templeteContent.children()[0]).clone();
            templete.attr("ismodel", "false");
            templete.find("[attoname=entityname]").val(Attachments[i].EntityName);
            templete.find("[attoname=attflag]").val(Attachments[i].AttFlag);
            templete.find("[attoname=recordid]").val(Attachments[i].RecordId);
            templete.find("[attoname=attachmentid]").val(Attachments[i].AttachmentId);
            templete.find("[attoname=attpath]").attr("src", Attachments[i].AttPath);
            if (templete.find("[attoname=attpath]").is("img")) {
                templete.find("[attoname=attpath]").attr(Attachments[i].AttFlag, Attachments[i].AttPath);
            }
            templete.find("[attoname=name]").text(Attachments[i].Name);
            templete.find("[attoname=attdesc]").html(Attachments[i].AttDesc);
            templete.find("[attoname=attribute1]").html(Attachments[i].Attribute1);
            templete.find("[attoname=attribute2]").html(Attachments[i].Attribute2);
            templete.find("[attoname=attribute3]").html(Attachments[i].Attribute3);
            templeteContent.append(templete);
        }

    }

    $("[name=attachment]").each(function () {
        var templete = $(this).find("[ismodel=true]");
        $(this).append(Attachment.AddButton.clone().attr("templete", templete.prop("outerHTML")));
        templete.remove();
    })
    initViewer();
}

function checkAttachment() { return true }

function SaveAttachmentRecord() {
    var check = checkAttachment();
    if (check != null && check == false)
        return;

    var entityData = {};
    entityData.attachmentid = document.getElementById("att_AttrachmentId_add").value;
    entityData.name = document.getElementById("att_name_add").value;
    entityData.attdesc = document.getElementById("att_desc_add").value;
    entityData.attpath = $("#att_image_add").attr("realsrc");
    entityData.attflag = document.getElementById("att_attflag_add").value;
    entityData.attribute1 = null;
    entityData.attribute2 = null;
    entityData.attribute3 = null;
    entityData.recordid = $.getQueryString("id");
    entityData.entityname = $.getQueryString("entityname");

    var json = JSON.stringify(entityData);
    console.log(json);

    $.ajax({
        type: "post",
        url: "/Service/Data/SaveData.ashx",
        data: {
            entityname: "attachment",
            data: json,
            id: entityData.attachmentid
        },
        dataType: "json",
        success: function (data, textStatus) {
            var templeteContent = $("[name=attachment][attflag=" + $("#att_attflag_add").val() + "]");
            var templete = null;
            var flag = 0
            $("[attoname=attachmentid]").each(function () {
                var t = $(this);
                if (t.val() == data.ResultData) {
                    templete = t.parent();
                }
            })
            if (templete == null) {

                var t = $("<div></div>").html(templeteContent.find(".addAttachmentButton").attr("templete"));

                templete = t.find("[ismodel=true]");
                flag = 1;
            }
            templete.attr("ismodel", "false");
            templete.find("[attoname=entityname]").val($.getQueryString("entityname"));
            templete.find("[attoname=attflag]").val($("#att_attflag_add").val());
            templete.find("[attoname=recordid]").val($.getQueryString("id"));
            templete.find("[attoname=attachmentid]").val(data.ResultData);
            templete.find("[attoname=attpath]").attr("src", $("#att_image_add").attr("src"));
            templete.find("[attoname=name]").text($("#att_name_add").val());
            templete.find("[attoname=attdesc]").html($("#att_desc_add").val());
            if (templete.find("[attoname=attpath]").is("img")) {
                templete.find("[attoname=attpath]").attr($("#att_attflag_add").val(), $("#att_image_add").attr("src"));
            }
            if (flag == 1) {
                templeteContent.find(".addAttachmentButton").before(templete);
            }

            initViewer();

            templete.show();
            //}

            $("[ismodel=true]").remove();

            $.toast("图片上传成功! ");
            Attachment.StyleJS.hidediv();
        },
        error: function (data) {
            for (var i in data) {
                console.log(i + ":" + data[i]);
            }

            $.toast("图片上传失败!");
        }
    });
}

function EditAttachment(data) {

    currentAttachment = $(data).parents("[ismodel]");
    var templete = $(currentAttachment);

    $("#att_attflag_add").val(templete.find("[attoname=attflag]").val());
    $("#att_AttrachmentId_add").val(templete.find("[attoname=attachmentid]").val());
    $("#att_image_add").attr("src", templete.find("[attoname=attpath]").attr("src"));
    $("#att_name_add").val(templete.find("[attoname=name]").text());
    $("#att_desc_add").val(templete.find("[attoname=attdesc]").text());


    $('#AddAttachment').show();
    $('#shade').show();
}


function DelAttachment(data) {


    currentAttachment = $(data).parents("[ismodel]");
    var templete = $(currentAttachment);

    var attachmentid = templete.find("[attoname=attachmentid]").val()
    $.ajax({
        type: "post",
        url: "/Service/Data/SoftDeleteItem.ashx",
        data: {
            entityname: "attachment",
            id: attachmentid
        },
        success: function (data, textStatus) {
            templete.remove();
            $.toast("删除成功! ");
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            $.toast("删除失败! ");
        }
    });
}








